package com.huan.nacos.listener;

import com.alibaba.cloud.nacos.refresh.NacosContextRefresher;
import com.alibaba.nacos.api.config.listener.AbstractSharedListener;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;
import org.apache.coyote.ProtocolHandler;
import org.apache.coyote.http11.Http11NioProtocol;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.beans.BeansException;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.cloud.endpoint.event.RefreshEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executor;

@Component
public class NacosConfigRefreshEventListener implements ApplicationListener<RefreshEvent>, ApplicationContextAware , TomcatConnectorCustomizer {
    private ServletWebServerApplicationContext applicationContext;

    @Override
    public void onApplicationEvent(RefreshEvent event) {
        TomcatWebServer webServer = (TomcatWebServer) applicationContext.getWebServer();

        AbstractSharedListener source = (AbstractSharedListener) event.getSource();
        String dataId = source.getDataId();

        // 可以判断那个nacos文件发送改变了，然后在修改tomcat线程池
        if (dataId.equals("?")) {
            ConfigurableEnvironment environment = applicationContext.getEnvironment();
            /**
             * #          enabled: true
             *   tomcat:
             *     threads:
             *       max: 300
             *       min-spare: 20
             */
            String maxThreads = environment.getProperty("server.tomcat.threads.max");
            String minSpare = environment.getProperty("server.tomcat.threads.min-spare");

            Tomcat tomcat = webServer.getTomcat();
            Connector connector = tomcat.getConnector();
            Http11NioProtocol protocolHandler = (Http11NioProtocol) connector.getProtocolHandler();
            protocolHandler.setMaxThreads(Integer.valueOf(maxThreads)); //设置tomcat线程处理请求线程数据最大
            protocolHandler.setMinSpareThreads(Integer.valueOf(minSpare));
            ThreadPoolExecutor executor = (ThreadPoolExecutor) protocolHandler.getExecutor();
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.RejectedExecutionHandler() {
//            @Override
//            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
//
//                System.out.println("r = " + r);
//            }
//        });
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = (ServletWebServerApplicationContext) applicationContext;
    }

    /**
     * 自定义 tomcat 线程池
     * @param connector the connector to customize
     */
    @Override
    public void customize(Connector connector) {
        //设置tomcat的线程池
//        connector.getProtocolHandler().setExecutor(null);
    }
}
